package com.java2207.group1.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java2207.group1.common.LayuiPageVo;
import com.java2207.group1.mapper.SiteMapper;
import com.java2207.group1.pojo.Site;
import com.java2207.group1.service.SiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("siteService")
public class SiteServiceImpl implements SiteService {

    @Autowired
    private SiteMapper siteMapper;
    @Override
    public LayuiPageVo<Site> sitePageData(Map<String, Object> params) {
        Integer page = Integer.parseInt(params.get("page") + "");
        Integer limit = Integer.parseInt(params.get("limit") + "");
        // mybatis 的分页查询
        PageHelper.startPage(page,limit);

        List<Site> sites = siteMapper.selectByParams(params);

        // mybatis 的分页数据结构
        PageInfo<Site> sitePageInfo = new PageInfo<>(sites);

        // 把 mybatis 的分页数据封装到 layui 的分页数据结构中
        LayuiPageVo<Site> siteLayuiPageVo = new LayuiPageVo<>();

        siteLayuiPageVo.setCode(0);  // layui 默认 0 是正确的状态码
        siteLayuiPageVo.setMsg("分页列表数据");  // 提示信息
        siteLayuiPageVo.setCount(sitePageInfo.getTotal());  // 总记录数
        siteLayuiPageVo.setData(sitePageInfo.getList());  // 分页的列表数据


        return siteLayuiPageVo;
    }

    @Override
    public int addSite(Site site) {
        return siteMapper.addSite(site);
    }

    @Override
    public int deleteSite(int id) {
        return siteMapper.deleteSite(id);
    }

    @Override
    public int updateSite(Site site) {
        return siteMapper.updateSite(site);
    }

    @Override
    public int batchSite(String ids) {
        String[] arr = ids.split(",");
        return siteMapper.batchSite(arr);
    }
}
